const BaseHandler     = require('../base-handler');
const SystemServices  = require('../../../constants').SystemServices;
const HTTP_VERBS      = require('../../../constants').HTTP_VERBS;

class SaveSettingHandler extends BaseHandler {
    get pattern() {
        return "/settings/:setting_name/save";
    }

    doGET(request, response) {
        response.redirect("/settings");
    }

    doPOST(request, response) {
        if (!request.session.user.authenticated) {
            response.json({
                success: false,
                error: '非法操作!'
            });
        } else {
            var settingName = request.params.setting_name;
            var settingValue = request.body;

            this.Controller.updateSystemSettingByName(request.session.user.id, settingName, settingValue, (result) => {
                response.json(result);
            });
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
    }
}

module.exports = SaveSettingHandler;